<script>
export default {
  name: "My",
  data(){
    return {
      username:""
    }
  },
  methods:{
    outLogin(){
      localStorage.clear();
      this.$router.push({
        path:"/login",
        query:{
          returnUrl:this.$route.fullPath
        }
      });
    }
  },
  beforeCreate() {
    console.log("beforeCreate");
  },
  created(){
    console.log("created");
  },
  beforeMount() {
    console.log("beforeMount");
  },
  mounted(){
    console.log("mounted");
  },
  beforeRouteEnter(to,from,next){
    console.log("beforeRouteEnter")
    // next("/login");// 可以实现类似于this.$router.push的作用
    if(localStorage.getItem("username")) {
      next(function(cm){
        cm.username = localStorage.getItem("username");
      })
    }
    else{
      next({
        path:"/login",
        query:{
          returnUrl:to.fullPath
        }
      })
    }

  }
}
</script>

<template>
  <div>
    <h3>欢迎<span style="color:red">{{username}}</span>的到来</h3>
    <button @click="outLogin">退出登陆</button>
  </div>

</template>

<style scoped>

</style>